const recursion = (targetArr, arr = []) => {
  targetArr?.forEach(t => {
    if (t?.listPeople && t?.listPeople?.length > 0) {
      arr.push(...t.listPeople);
    }
    if (t?.userId) {
      arr.push(t);
    }
    if (t?.childList && t?.childList?.length > 0) recursion(t.childList, arr);
  });
  return arr;
};

const unique = arr => {
  let newArr = [];
  arr.forEach(function (item) {
    if (newArr.findIndex(t => t.userId === item.userId) === -1) {
      newArr.push(item);
    }
  });
  return newArr;
};

const getPeoples = arr => {
  let peopleList = recursion(arr);
  peopleList = unique(peopleList);
  return peopleList;
};

export default getPeoples;
